// index.js

Page({
    data: {
        initPageY: 0,
        stepSizeY: 0,
        username: '',
        avatarUrl: '',
        openid: '',
        backImg: '',
        img: ''
    },
    //   登录账户
    login() {
        const that = this
        const username = wx.getStorageSync('username')
        const avatarUrl = wx.getStorageSync('avatarUrl')
        if (username && avatarUrl) {
            this.setData({
                username,
                avatarUrl
            })
        } else {
            Promise.all([this.getOpenId(), this.getUserProfile()]).then(res => {
                const [{
                    result: {
                        openid
                    }
                }, {
                    userInfo: {
                        nickName: username,
                        avatarUrl
                    }
                }] = res
                wx.cloud.callFunction({
                    name: 'getUser',
                    data: {
                        openid
                    },
                    success(res) {
                        if (res) {
                            const Getusername = res.result.result.data[0].username
                            const GetavatarUrl = res.result.result.data[0].avatarUrl
                            const Getopenid = res.result.result.data[0].openid
                            let GetbackImg = res.result.result.data[0].backImg.fileID
                            that.temporaryUrl(GetbackImg, Getopenid)
                            that.setData({
                                username: Getusername,
                                avatarUrl: GetavatarUrl,
                                openid: Getopenid
                            })
                            wx.setStorageSync('username', Getusername)
                            wx.setStorageSync('avatarUrl', GetavatarUrl)
                            wx.setStorageSync('openid', Getopenid)
                        }
                    },
                    fail() {
                        wx.cloud.callFunction({
                            name: "login",
                            data: {
                                userInfo: {
                                    openid: openid,
                                    username: username,
                                    avatarUrl: avatarUrl,
                                    backImg: {
                                        url: '',
                                        fileID: ''
                                    }
                                }
                            },
                            success(res) {
                                wx.setStorageSync('username', username)
                                wx.setStorageSync('avatarUrl', avatarUrl)
                                wx.setStorageSync('openid', openid)
                                that.setData({
                                    username,
                                    avatarUrl,
                                    openid
                                })
                            }
                        })
                    }
                })

            })
        }
    },
    // 获取openid
    getOpenId() {
        return wx.cloud.callFunction({
            name: "getOpenId"
        })
    },
    // 获取头像昵称
    getUserProfile() {
        return wx.getUserProfile({
            lang: 'zh_CN',
            desc: '获取头像和昵称'
        })
    },
    // 更换背景图
    changeBack() {
        const updateImgTime = wx.getStorageSync('updateImgTime')
        if (new Date().getTime() - updateImgTime < 604800000) {
            wx.showToast({
                icon: 'none',
                title: '距离上次更换封面不足一周，请不要频繁更换！'
            })
            return false
        }
        wx.showLoading({
            title: '上传中...',
            mask: true
        })
        const that = this
        const openid = wx.getStorageSync('openid')
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            success(res) {
                wx.cloud.uploadFile({
                    cloudPath: `${openid}/${new Date().getTime()}backImg.jpg`,
                    filePath: res.tempFiles[0].tempFilePath,
                    success(res) {
                        wx.setStorageSync('backImg', res.fileID)
                        that.temporaryUrl(res.fileID, openid)
                        wx.cloud.callFunction({
                            name: "login",
                            data: {
                                userInfo: {
                                    openid: openid,
                                    backImg: {
                                        url: res.fileID,
                                        fileID: res.fileID
                                    }
                                }
                            },
                            success(res) {
                                wx.hideLoading()
                                wx.showToast({
                                    icon: 'success',
                                    title: '更换成功！'
                                })
                                wx.setStorageSync('updateImgTime', new Date().getTime())
                            }
                        })
                    }
                })
            },
            fail() {
                wx.hideLoading()
            }
        })
    },
    touchstartHandle(e) {
        this.setData({
            initPageY: e.touches[0].pageY
        })
    },
    // 背景图临时链接
    temporaryUrl(cloud, openId) {
        const that = this
        wx.cloud.getTempFileURL({
            fileList: [{
                fileID: cloud,
                tempFileURL: `${openId}/`
            }]
        }).then(res => {
            that.setData({
                backImg: res.fileList[0].tempFileURL
            })
        }).catch(res => {
            that.setData({
                img: 'https://7065-pen-v7ngb-1259410365.tcb.qcloud.la/SC01C25.jpg?sign=15327331bfce4727799ba27971e3082e&t=1650288271'
            })
        })
    },
    touchmoveHandle(e) {
        let stepSizeY = e.changedTouches[0].pageY - this.data.initPageY
        if (stepSizeY < 0) {
            stepSizeY = 0
        } else if (stepSizeY > 280) {
            stepSizeY = 280
        }
        this.setData({
            stepSizeY: stepSizeY
        })
    },
    touchendHandle(e) {
        this.setData({
            initPageY: 0,
            stepSizeY: 0,
        })
    },
    onReady() {
        const that = this
        const username = wx.getStorageSync('username')
        const avatarUrl = wx.getStorageSync('avatarUrl')
        const openid = wx.getStorageSync('openid')
        if (!openid) return false
        wx.cloud.callFunction({
            name: 'getUser',
            data: {
                openid
            },
            success(res) {
                const backImg = res.result.result.data[0].backImg.fileID
                that.temporaryUrl(backImg, openid)
            },
            fail(res) {
                console.log(res)
            }
        })
        if (username && avatarUrl && openid) {
            this.setData({
                username,
                avatarUrl,
                openid
            })
        }
    },
    // 我的文章
    myArticle() {
        wx.navigateTo({
            url: "/delPages/myArtcle/index"
        })
    },
    star() {
        wx.showToast({
            title: '等待开发~',
            icon: 'none'
        })
    },
    collection() {
        wx.showToast({
            title: '等待开发~',
            icon: 'none'
        })
    }
});